﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Script;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DWF.Studio.Views
{
    /// <summary>
    /// 属性区域控件
    /// </summary>
    public partial class UCtrlPropertyRegion : UserControl
    {
        public UCtrlPropertyRegion()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                init();
            }
        }

        private void init()
        {
            GlobalActionHandler.ScriptSelected += changeFile;
            GlobalActionHandler.ProjectClosing += closeProject;

            GlobalActionHandler.PackageChanging += packageChange;

            GlobalActionHandler.SwitchAttributePanel += openAttributePanel;
            GlobalActionHandler.SwitchOutlinePanel += openOutlinePanel;
        }

        private void changeFile(TagEventArgs<ScriptBase> e)
        {
            if (e.Tag != null)
            {
                this.propertyBorder.Child = e.Tag.PropertyBar1;
                this.outlineBorder.Child = e.Tag.PropertyBar2;
            }
            else
            {
                this.propertyBorder.Child = null;
                this.outlineBorder.Child = null;
            }
        }

        private void closeProject(EventArgs e)
        {
            this.propertyBorder.Child = null;
            this.outlineBorder.Child = null;
        }

        private void packageChange(EventArgs e)
        {
            this.propertyBorder.Child = null;
            this.outlineBorder.Child = null;
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabCtrl.Items.Count == 2)
            {
                if (tabCtrl.SelectedIndex == 0)
                {
                    this.propertyBorder.Child?.Refresh();
                }
                else if (tabCtrl.SelectedIndex == 1)
                {
                    this.outlineBorder.Child?.Refresh();
                }
            }
        }

        private void openAttributePanel(EventArgs e)
        {
            if (tabCtrl.SelectedIndex == 0)
            {
                GlobalActionHandler.ChangePropertyRegionWidth?.Invoke(new TagEventArgs<bool>(true));
            }
            else
            {
                tabCtrl.SelectedIndex = 0;
                GlobalActionHandler.ChangePropertyRegionWidth?.Invoke(new TagEventArgs<bool>(false));
            }
        }
        private void openOutlinePanel(EventArgs e)
        {
            if (tabCtrl.SelectedIndex == 1)
            {
                GlobalActionHandler.ChangePropertyRegionWidth?.Invoke(new TagEventArgs<bool>(true));
            }
            else
            {
                tabCtrl.SelectedIndex = 1;
                GlobalActionHandler.ChangePropertyRegionWidth?.Invoke(new TagEventArgs<bool>(false));
            }
        }
    }
}
